package xainchenggaoji;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Mr.ma
 * @since 2022/11/2 20:51
 * 创建线程的第三种方式，实现Callable接口，该方式创建线程存在返回值
 **/
public class CreateThread03 {
    public static void main(String[] args) {
        TestCallable callableTest = new TestCallable();
        FutureTask<Integer> integerFutureTask = new FutureTask<Integer>(callableTest);

        Thread thread = new Thread(integerFutureTask);
        thread.start();

        try {
            System.out.println(integerFutureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class TestCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }
}



